#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>

using std::cout;
using std::endl;
using std::vector;
using std::for_each;
using std::remove_if;
using std::ostream_iterator;
using std::copy;
using std::bind1st;
using std::bind2nd;

bool func(int value)
{
    return value > 4;
}
void test()
{
    vector<int> number = {1, 3, 5, 8, 9, 4, 6, 8, 3};
    for_each(number.begin(), number.end(), 
             [](int &value)
             { 
             cout << value << "  " ;
             });
    cout << endl;

    /* auto it = remove_if(number.begin(), number.end(), */ 
    /*                     bind1st(std::less<int>(), 4)); */
    auto it = remove_if(number.begin(), number.end(), 
                        bind2nd(std::greater<int>(), 4));
    
    /* auto it = remove_if(number.begin(), number.end(), [](int value){ */
    /*                     return value > 4; */
    /*                     }); */
    //remove_if不会一次将满足func的条件的元素删除，但是会返回一个
    //迭代器，该迭代器以及以后的元素可以后续删除
    //remove_if往往会与erase结合使用
    /* auto it = remove_if(number.begin(), number.end(), func); */
    number.erase(it, number.end());
    copy(number.begin(), number.end(), 
         ostream_iterator<int>(cout, "  "));
    cout << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

